package javagamecontroller.activities;

import java.util.HashMap;
import java.util.Map;
import util.input.android.events.AndroidServerMessage;
import javagamecontroller.activities.controllers.ControllerFrame;
import javagamecontroller.activities.controllers.GridController;
import javagamecontroller.activities.controllers.JoystickController;
import javagamecontroller.activities.controllers.TouchController;
import javagamecontroller.connection.BluetoothMessenger;
import com.javagamecontroller.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * In this activity a list of all controllers are shown. The game server can
 * specify which controllers are supported.
 * 
 * @author Ben Schwab
 * 
 */

public class SelectController extends Activity implements OnClickListener, OnLongClickListener {
    /**
     * Constant used for interactivity processing
     */
    public static final String ACTION = "action";
    /**
     * Constant used to signal a config change has occured
     */
    public static final int CONFIG_CHANGE = 1;
    /**
     * Constant used to signal a controller description has occured
     */
    public static final int DESCRIPTION_CHANGE = 2;
    private Button myGameBoyButton;
    private Button myPlayStationButton;
    private Button myTouchControllerButton;
    private boolean[] myConfigSettings;
    private ImageView myAccelerometerEnabledIcon;
    private BluetoothMessenger myMessenger;
    private Map<String, String> myControllerDescriptions = new HashMap<String, String>();

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_controller);
        myGameBoyButton = (Button) findViewById(R.id.button1);
        myGameBoyButton.setOnClickListener(this);
        myGameBoyButton.setOnLongClickListener(this);
        myPlayStationButton = (Button) findViewById(R.id.button2);
        myPlayStationButton.setOnClickListener(this);
        myPlayStationButton.setOnLongClickListener(this);
        myTouchControllerButton = (Button) findViewById(R.id.button3);
        myTouchControllerButton.setOnClickListener(this);
        myTouchControllerButton.setOnLongClickListener(this);
        myAccelerometerEnabledIcon = (ImageView) findViewById(R.id.accelerometer_icon);
        myConfigSettings = new boolean[] { true, true, true, true };
        myMessenger = BluetoothMessenger.getInstance();
        boolean[] configSettings = myMessenger.getControllerConfigs();
        changeConfigSettings(configSettings);
        myControllerDescriptions = myMessenger.getControllerDescriptions();
        myMessenger.subscribeToControllerConfigChanges(this, handler);
        displayOptions();

    }

    public void onClick (View v) {
        Intent i;
        switch (v.getId()) {
            case R.id.button1:
                i = new Intent(this, GridController.class);
                i.putExtra(GridController.BUTTON_MANAGER,
                           "javagamecontroller.activities.controllers.GameBoyButtonManager");
                i.putExtra(GridController.BACKGROUND, R.drawable.gameboy_background);
                i.putExtra(ControllerFrame.ACCEL_CONFIG, myConfigSettings[3]);

                startActivity(i);
                break;
            case R.id.button2:
                i = new Intent(this, JoystickController.class);
                i.putExtra(GridController.BUTTON_MANAGER,
                           "javagamecontroller.activities.controllers.PlaystationButtonManager");
                i.putExtra(ControllerFrame.ACCEL_CONFIG, myConfigSettings[3]);
                startActivity(i);
                break;
            case R.id.button3:
                i = new Intent(this, TouchController.class);
                i.putExtra(ControllerFrame.ACCEL_CONFIG, myConfigSettings[3]);
                startActivity(i);
                break;
        }

    }

    @Override
    public void onResume () {
        super.onResume();
        myMessenger = BluetoothMessenger.getInstance();
        if (myMessenger.getState() != BluetoothMessenger.State.Connected) {
            this.finish();
        }
        changeConfigSettings(myMessenger.getControllerConfigs());
        displayOptions();

    }
    /**
     * Display the controllers that are available
     */
    public void displayOptions () {
        if (myConfigSettings == null) { return; }
        if (myConfigSettings[0]) {
            myGameBoyButton.setVisibility(View.VISIBLE);
        }
        else {
            myGameBoyButton.setVisibility(View.GONE);
        }
        if (myConfigSettings[1]) {
            myPlayStationButton.setVisibility(View.VISIBLE);
        }
        else {
            myPlayStationButton.setVisibility(View.GONE);
        }
        if (myConfigSettings[2]) {
            myTouchControllerButton.setVisibility(View.VISIBLE);
        }
        else {
            myTouchControllerButton.setVisibility(View.GONE);
        }
        if (myConfigSettings[3]) {
            myAccelerometerEnabledIcon.setVisibility(View.VISIBLE);
        }
        else {
            myAccelerometerEnabledIcon.setVisibility(View.GONE);
        }
    }
    /**
     * 
     * @param configSettings the config settings of the controller
     */
    public void changeConfigSettings (boolean[] configSettings) {
        if (configSettings != null) {
            myConfigSettings = configSettings;
        }
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage (Message msg) {
            switch (msg.getData().getInt(ACTION)) {
                case CONFIG_CHANGE:
                    displayOptions();
                    break;
                case DESCRIPTION_CHANGE:
                    changeDescriptions();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };

    private void changeDescriptions () {

    }

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            myMessenger.connectionLost();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 
     * @param myControllerDescriptions
     */
    public void changeControllerDescriptions (Map<String, String> controllerDescriptions) {
        myControllerDescriptions = controllerDescriptions;
    }

    @Override
    public boolean onLongClick (View v) {
        switch (v.getId()) {
            case R.id.button1:
                if (myControllerDescriptions.containsKey(AndroidServerMessage.GAMEBOY_DESCRIPTION)) {
                    showControllerInfo(myControllerDescriptions
                            .get(AndroidServerMessage.GAMEBOY_DESCRIPTION));
                }

                break;
            case R.id.button2:
                if (myControllerDescriptions
                        .containsKey(AndroidServerMessage.PLAYSTATION_DESCRIPTION)) {
                    showControllerInfo(myControllerDescriptions
                            .get(AndroidServerMessage.PLAYSTATION_DESCRIPTION));
                }

                break;
            case R.id.button3:
                if (myControllerDescriptions
                        .containsKey(AndroidServerMessage.TOUCHSCREEN_DESCRIPTION)) {
                    showControllerInfo(myControllerDescriptions
                            .get(AndroidServerMessage.TOUCHSCREEN_DESCRIPTION));
                }

                break;
        }
        return false;
    }

    private void showControllerInfo (String text) {
        Dialog d = new Dialog(this);
        d.setContentView(R.layout.help_dialogue);
        d.setTitle("Controls Info");
        TextView helpText = (TextView) d.findViewById(R.id.help_dialogue);
        helpText.setText(text);
        d.show();

    }

}
